#include "RandomDie.h"
#include <stdio.h>
#include <Windows.h>
#include <ctime>

void RandomDie::Draw(int a_r)
{
	int offsY = y; // Y offset
	gotoXY(x, y);

	// Draw the top of the die
	// gotoXY necessary because \n doesn't work right like this
	printf("  _______"); gotoXY(x, ++offsY);
	printf(" /______/|"); gotoXY(x, ++offsY);

	// Draw the middle of the die
	switch(a_r)
	{
	case 1:
		printf("|       ||"); gotoXY(x, ++offsY);
		printf("|   *   ||"); gotoXY(x, ++offsY);
		printf("|       ||"); gotoXY(x, ++offsY);
		break;
	case 2:
		printf("|     * ||"); gotoXY(x, ++offsY);
		printf("|       ||"); gotoXY(x, ++offsY);
		printf("| *     ||"); gotoXY(x, ++offsY);
		break;
	case 3:
		printf("|     * ||"); gotoXY(x, ++offsY);
		printf("|   *   ||"); gotoXY(x, ++offsY);
		printf("| *     ||"); gotoXY(x, ++offsY);
		break;
	case 4:
		printf("| *   * ||"); gotoXY(x, ++offsY);
		printf("|       ||"); gotoXY(x, ++offsY);
		printf("| *   * ||"); gotoXY(x, ++offsY);
		break;
	case 5:
		printf("| *   * ||"); gotoXY(x, ++offsY);
		printf("|   *   ||"); gotoXY(x, ++offsY);
		printf("| *   * ||"); gotoXY(x, ++offsY);
		break;
	case 6:
		printf("| * * * ||"); gotoXY(x, ++offsY);
		printf("|       ||"); gotoXY(x, ++offsY);
		printf("| * * * ||"); gotoXY(x, ++offsY);
		
		break;
	default:
		printf("Error: Die did not roll between 1 and 6\n");
		break;
	}

	// Draw the bottom of the die
	printf("|_______|/");

}

RandomDie::RandomDie (int a_x, int a_y)
{
	x = a_x;
	y = a_y;
}

// Roll x number of times before stopping
int RandomDie::Roll(int times)
{
	int rnd;

	for(int i = 0; i < times; i++)
	{
		Sleep(50);
		rnd = (rand()%6 + 1); // Roll between 1 and 6
		Draw(rnd);
	}

	// Return whatever number we landed on
	return rnd;
}